package com.stefan.recursion;
//阶乘 f(n) = n*f(n-1)
public class Demo1 {
    public int fac(int n) {
        if (n == 1){
            return 1;
        }
        return n * fac(n-1);
    }

    //尾递归，就是完全的递归方法调用，没有运算逻辑，这样效率高
    public int fac(int n,int value) {
        if (n == 1){
            return 1;
        }
       return fac(n-1,n*value);
    }

    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        long start = System.currentTimeMillis();
        int fac = demo1.fac(5);
        System.out.println("结果： " + fac + " 耗时 "+(System.currentTimeMillis() - start));

        int fac1 = demo1.fac(5,1);
        System.out.println("结果： " + fac + " 耗时 "+(System.currentTimeMillis() - start));
    }
}
